package com.example.yuejian23.yuejian_23.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejian23.yuejian_23.Data.Mdata;
import com.example.yuejian23.yuejian_23.R;
import com.example.yuejian23.yuejian_23.javaBean.GerenBean;
import com.example.yuejian23.yuejian_23.javaBean.Result;
import com.example.yuejian23.yuejian_23.model.UserModel;
import com.example.yuejian23.yuejian_23.service.UserService;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class XiugaiGerenActivity extends AppCompatActivity {

    ImageView touxiang;
    EditText nichen,tel;
    Button xiugai;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xiugai_geren);
        initView();

        initEvents();
        geren();
    }
    private void initView() {
        nichen=(EditText) findViewById(R.id.nichen);
        tel=(EditText) findViewById(R.id.tel);
        xiugai=(Button)findViewById(R.id.xiugai);
        xiugai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                xiugaigeren();
            }


        });
    }


    private void initEvents() {

    }


    private void geren() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://115.159.93.244/new18yuejian/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        UserService userService=retrofit.create(UserService.class);
        Call<GerenBean> call = userService.getuserinfo(Mdata.uid);;
        call.enqueue(new Callback<GerenBean>() {


            @Override
            public void onResponse(Call<GerenBean> call, Response<GerenBean> response) {
                GerenBean geren=response.body();
                nichen.setHint(geren.getUname());
                //nichen.setText(geren.getUname());
                tel.setText(geren.getUtel());
//                Picasso.with(XiugaiGerenActivity.this).load("http://115.159.93.244/new18yuejian/Public/upload/"+geren.getUserpic()).into(touxiang);
            }

            @Override
            public void onFailure(Call<GerenBean> call, Throwable t) {
                System.out.print(toString());
            }
        });
    }
    private void xiugaigeren() {
        String userId ;
        Intent intent=getIntent();
        Bundle bundle = intent.getExtras();
        userId=bundle.getString("userid");
        if (userId != null) {
            String userName = nichen.getText().toString();
            String userPhone = tel.getText().toString();
            UserModel userModel = new UserModel();
            Call<Result> call = userModel.saveUserInfoById(userId, userName, userPhone);
            call.enqueue(new Callback<Result>() {
                @Override
                public void onResponse(Call<Result> call, Response<Result> response) {
                    Result result = response.body();
                    if (result != null) {
                        if (result.getCode() == 1) {
                            Toast.makeText(XiugaiGerenActivity.this, "修改成功", Toast.LENGTH_LONG).show();
                            finish();
                        }
                    } else {
                        Toast.makeText(XiugaiGerenActivity.this, "修改失败", Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                public void onFailure(Call<Result> call, Throwable t) {
                    Toast.makeText(XiugaiGerenActivity.this, "修改失败", Toast.LENGTH_LONG).show();
                }
            });
        }
    }
}
